package com.micro.school.h5.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class H5HeaderConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        
        // 配置跨域设置
        config.setAllowCredentials(true);  // 允许携带凭证
        config.addAllowedOriginPattern("*");  // 允许的前端地址
        config.addAllowedHeader("*");     // 允许所有请求头
        config.addAllowedMethod("*");     // 允许所有请求方法
        config.setMaxAge(3600L);          // 预检请求缓存时间

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);  // 对所有路径生效

        return new CorsWebFilter(source);
    }
}